package movgen;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import state.GameState;

public class Util {
	
	
	public  static List<Integer> getLegalMoveList(GameState state) 
	{
	 
		int[] movelist = state.getAllMoves();
		List<Integer> moveList2=new ArrayList<Integer>();
		
		int flags2=state.flags;
		long hash=state.hash;
		
		//GameState.resetMoveHistory();
		for (int i=0; i < movelist.length; i++)
		{
			int moveType = Move2.moveType(movelist[i]);
			if (moveType == Move2.kcastle || moveType == Move2.qcastle) {    //castling
				if (state.isInCheck( state.color)) {
					continue;
				}
			}
			state.make(movelist[i]);

			// you cannot make a move that puts yourself in check
			if (state.isInCheck(  1- state.color)) {
				//System.out.println("bad move " + Move2.toString(movelist[i]));
				//System.out.println("you cannot make a move that puts yourself in check");
				state.unmake(movelist[i], flags2, hash);

				continue;
			}
		
			
			if (moveType == Move2.kcastle || moveType == Move2.qcastle) {    //castling
				if (state.isInCheck( 1-state.color)) {
					state.unmake(movelist[i], flags2, hash);
					continue;
				}
				if (state.isInCheck(  GameState.minedBitboards[1-state.color][moveType-2],  state.color) )  {
					state.unmake(movelist[i], flags2, hash);
					continue;
				}
			}
			state.unmake(movelist[i], flags2, hash);
			moveList2.add(movelist[i]);
		}
		  //System.out.println("Finished Generating moves");
		return moveList2;
	}
	
	public static char getPiece(int piecetype) {
		switch (piecetype) {
		case GameState.WP : return 'P';
		case GameState.BP : return 'p';
		
		case GameState.WN : return 'N';
		case GameState.BN : return 'n';
		
		case GameState.WB : return 'B';
		case GameState.BB : return 'b';
		
		case GameState.WR : return 'R';
		case GameState.BR : return 'r';
		
		case GameState.WQ : return 'Q';
		case GameState.BQ : return 'q';
		
		case GameState.WK : return 'K';
		case GameState.BK : return 'k';
		}
		return '-';
	}
	public static int getPieceReverse(char piecetype) {
		switch (piecetype) {
		case 'P' : return GameState.WP ;
		case 'p' : return GameState.BP ;
		
		case 'N' : return GameState.WN;
		case 'n' : return GameState.BN;
		
		case 'B' : return GameState.WB;
		case 'b' : return GameState.BB;
		
		case 'R': return GameState.WR;
		case 'r': return GameState.BR;
		
		case 'Q': return GameState.WQ;
		case 'q': return GameState.BQ;
		
		case 'K': return GameState.WK;
		case 'k': return GameState.BK;
		}
		return GameState.EMPTY;
	}
	public static void printMoveList(List<Integer>list) {
		System.out.println("PRINT MOVELIST");
		Iterator<Integer> iter = list.iterator();
		while (iter.hasNext()) {
			int move = iter.next();
			Move2.toString(move);
			
		}
		System.out.println("DONE PRINT MOVELIST");
	}
	
	public static String toCoOrdinate(byte index)
	{
		return toFile(index) + "" + toRank(index);
	}

	/**
	 * Gets the file of the given board[] index
	 *
	 * @param   index   array index of position [0-63]
	 * @return  file  of the position ['a'-'h']
	 */
	public static char toFile(byte index)
	{
		char file = 'a';
		file += toFileIndex(index); // += 0-7
		return file;
	}


	/**
	 * Gets the rank of the given board[] index
	 *
	 * @param   index   array index of position [0-63]
	 * @return  rank of the position [1-8]
	 */
	public static int toRank(byte index)
	{
		return (8 - index/8); // 1-8
	}

	/**
	 * Gets the file index of the given board[] index
	 *
	 * @param   index   array index of position [0-63]
	 * @return  file index of the position [0-7]
	 */
	public static int toFileIndex(int index)
	{
		return (index % 8); // 0-7
	}
	public static int getRow(int position) {
		return (int)(position / 8);
	}
	
	private static int getIndex(int row, int col) {   //row is from 0 to 7
		return (row*8) + col;
	}
	public static char[]  convertRowsToBoard(String[] rows) 
	{
		char board[]= new char[64];
		int index=0;
		for (int i=0; i < rows.length; i++) {
			//System.out.println(rows[i]);
			
			char board1[] = rows[i].toCharArray();
			
			for (int j=0; j < board1.length; j++) 
			{
				if (Character.isLetter(board1[j])) 
				{
					board[index++]= board1[j];
					
				}
				else {
					if (Character.isDigit(board1[j])) 
					{
						int x = Character.getNumericValue(board1[j]);
						for (int k=0; k< x; k++ )
						   board[index++]= '-';
					}
				}
			}
		}
		return board;
	}
	
	public static int getIndexFromSquare(String square)  //square is like e2 in the expression e2e4
	{
		char fromColC = square.charAt(0); //like e
		int sourceColumn  = fromColC - 97;
		int sourceRow = 7 - ( Character.getNumericValue(square.charAt(1)) -1 );
		int index = Util.getIndex(sourceRow, sourceColumn);
		return index;
	}
	
	public static void main(String[] args) {
		String s[] = { "e5", "d6",  "c4","d3" };
		for (int i=0; i <s.length; i++) {
			System.out.println(getIndexFromSquare(s[i]) + "  ");
		}
	}

	public static int[] convertCharBoardToIntBoard(char[] board1) {
		int[] board2 = new int[64];
		for (int i=0; i < 64; i++) {
			board2[i] = Util.getPieceReverse(board1[i]) ;
		}
		return board2;
	}
	public static void error(GameState gs, GameState state, int move) {
		if (!gs.equals(state)) {
			System.out.println("error");
			for (int i=0; i < 64; i++) {
				if (gs.board[i] != state.board[i] ) {
					System.out.println(" error at board i = " + i);
				}
			}
			for (int i=0; i < 14; i++) {
				if (gs.bitboard[i] != state.bitboard[i] ) {
					System.out.println(" error at bitboard i = " + i);
				}
			}
		}
		if (!gs.equals(state)) {
			System.out.println("error");
		}
		System.out.println(Move2.toString(move));
		gs.print();
		state.print();

	}
	public static void sort(int array[])
	{
		for (int i = 1; i < array.length; i++)
		{
			int j = i;
			int x = array[i];

			while ((j > 0) && (array[j-1] > x))
			{
				array[j] = array[j-1];
				j--;
			}
			array[j] = x;
		}
	}
}
